package com.jgzx.domain;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

/**
 * @author libo
 * @date 2021/9/15 14:22
 */
@ApiModel(value = "高级查询DTO")
@Accessors(chain = true)
@Data
public class SeniorQueryDTO extends PageDTO implements Serializable {

    @ApiModelProperty(value = "关键字")
    private String key_word;

    @ApiModelProperty(value = "关联ID", example = "null")
    @Min(value = 1, message = "ID不能为负数和0")
    private Long relation_id;

    @ApiModelProperty(value = "关联类型")
    private String relation_type;

    @ApiModelProperty(value = "方案ID", example = "null")
    @Min(value = 1, message = "ID不能为负数和0")
    private Long scheme_id;

    @ApiModelProperty(value = "排序字段")
    private String sort_field;

    @ApiModelProperty(value = "排序  0-正序, 1-倒序" , allowableValues = "0, 1")
    private Integer order;

    @ApiModelProperty(value = "别名", hidden = true)
    private String alias;

    @Valid
    @ApiModelProperty(value = "高级查询列表")
    private List<SeniorDetail> SeniorDetailList;

    @ApiModel(value = "高级查询字段明细")
    @Accessors(chain = true)
    @Data
    public static class SeniorDetail implements Serializable{

        @ApiModelProperty(value = "查询字段")
        @NotEmpty(message = "查询字段不能为空")
        private String query_field;

        @Valid
        @ApiModelProperty(value = "字段类型")
        @NotNull(message = "字段类型不能为空")
        private fieldType field_type;

        @ApiModelProperty(value = "操作符")
        @NotNull(message = "操作符不能为空")
        private FieldOperaEnum opera_symbol;

        @ApiModelProperty(value = "查询值")
        @NotEmpty(message = "查询字段值不能为空")
        private List<String> field_values;

    }

    @ApiModel(value = "字段类型")
    @Accessors(chain = true)
    @Data
    public static class fieldType implements Serializable {

        @ApiModelProperty(value = "精度")
        private String precision;

        @ApiModelProperty(value = "字段类型")
        @NotEmpty(message = "字段类型必填")
        private String ref;

        @ApiModelProperty(value = "前端输入类型")
        private String type;
    }

    @ApiModel(value = "字段操作枚举")
    public enum FieldOperaEnum {
        /**
         * 等于, IN
         */
        EQ(1, "="),
        /**
         * 不等于, NOT IN
         */
        NE(2, "!="),
        /**
         * 包含
         */
        LIKE(3, "LIKE"),
        /**
         * 不包含
         */
        NOT_LIKE(4, "NOT LIKE"),
        /**
         * 大于
         */
        GT(5, ">"),
        /**
         * 小于
         */
        LT(6, "<"),
        /**
         * 大于等于
         */
        GE(7, ">="),
        /**
         * 小于等于
         */
        LE(8, "<="),
        /**
         * 为空
         */
        IS_NULL(9, "IS NULL"),
        /**
         * 不为空
         */
        IS_NOT_NULL(10, "IS NOT NULL"),
        /**
         * 介于
         */
        BETWEEN(11, "BETWEEN"),
        ;

        private final Integer opera_symbol;

        private final String value;

        FieldOperaEnum(Integer opera_symbol, String value) {
            this.opera_symbol = opera_symbol;
            this.value = value;
        }

        @JsonCreator
        public static FieldOperaEnum create(Integer opera_symbol) {
            for (FieldOperaEnum fieldOperaEnum : values()) {
                if (fieldOperaEnum.getOperaSymbol().equals(opera_symbol)) {
                    return fieldOperaEnum;
                }
            }
            return null;
        }

        public Integer getOperaSymbol() {
            return this.opera_symbol;
        }

        public String getValue() {
            return this.value;
        }
    }

    public String getKey_word() {
        return StrUtil.trim(key_word);
    }

    public void setKey_word(String key_word) {
        this.key_word = key_word;
    }

}
